<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <!-- logo区域 -->
      <div class="login-head">
        <div class="logo"></div>
        <!-- 表单区域 -->
      </div>
      <el-form class="login-form" ref="form" :model="form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>

        <el-form-item prop="isAgree">
            <el-checkbox v-model="form.isAgree" label="我已阅读" name="type"></el-checkbox>
        </el-form-item>

        <el-form-item>
        <el-button :loading="isLoading" class="login-btn" type="primary" @click="onSubmit">登录</el-button>
      </el-form-item>
      </el-form>
    </div>
  </div>

</template>
<script>
export default {
  name: 'login',
  data () {
    return {
      isLoading: false,
      form: {
        mobile: '13911111111',
        code: '246810',
        isAgree: true
      },

      rules: {
        // 应用规则
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3|4|5|7|8][0-9]{9}$/, message: '请输入合法的手机号!!', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '请输入合法的验证码!!', trigger: 'blur' }
        ],
        isAgree: [
          {
            // 通过规则校验之后就会调用callback(),没有通过则会调用callback的错误对象
            validator: function (rule, value, callback) {
              console.log(rule, value, callback)
              if (value) {
                callback()
              } else {
                callback(new Error('你不同意?那不太行'))
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    async doLogin () {
      const { mobile, code } = this.form
      try {
        const res = await this.$http({
          url: 'mp/v1_0/authorizations',
          method: 'POST',
          data: {
            mobile,
            code
          }
        })
        localStorage.setItem('tokenStr', res.data.data.token)
        console.log(res)
        // alert('成功喽,芜湖!')
        this.$message.success('pxj乐园!')
        this.isLoading = false
        this.$router.push('/')
      } catch (err) {
        console.log(err)
        this.$message.error('因为你不会，所以你才会')
        console.log('ajax出错啦啦啦')
        console.log(err)
        this.isLoading = false
      }
    },
    // doLogin () {
    //   const { mobile, code } = this.form
    //   console.log(mobile, code)

    //   this.isLoading = true
    //   this.$http({
    //     url: 'mp/v1_0/authorizations',
    //     method: 'POST',
    //     data: {
    //       mobile,
    //       code
    //     }
    //   }).then(res => {
    //     localStorage.setItem('tokenStr', res.data.data.token)
    //     console.log(res)
    //     // alert('成功喽,芜湖!')
    //     this.$message.success('pxj乐园!')
    //     this.isLoading = false
    //     this.$router.push('/')
    //   }).catch(err => {
    //     console.log('出错啦')
    //     console.log(err)
    //     this.$message.error('因为你不会，所以你才会')
    //     this.isLoading = false
    //   })
    // },
    onSubmit () {
      // 手动校验,看元素是否通过了rul
      this.$refs.form.validate(valid => {
        valid && this.doLogin()
      // alert('真有你的')
      })

      // console.log('submit!')
    }
  }
}
</script>
<style scoped lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("../../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
